﻿/*
 Copyright aswing.org, see the LICENCE.txt.
*/

import org.aswing.awml.AwmlNamespace;
import org.aswing.awml.AwmlParser;
import org.aswing.awml.ButtonGroupManager;
import org.aswing.Box;
import org.aswing.ButtonGroup;
import org.aswing.Component;
import org.aswing.Container;
import org.aswing.JAccordion;
import org.aswing.JAdjuster;
import org.aswing.JAttachPane;
import org.aswing.JButton;
import org.aswing.JCheckBox;
import org.aswing.JComboBox;
import org.aswing.JFrame;
import org.aswing.JLabel;
import org.aswing.JList;
import org.aswing.JLoadPane;
import org.aswing.JPanel;
import org.aswing.JProgressBar;
import org.aswing.JRadioButton;
import org.aswing.JScrollBar;
import org.aswing.JScrollPane;
import org.aswing.JSeparator;
import org.aswing.JSlider;
import org.aswing.JSpacer;
import org.aswing.JTabbedPane;
import org.aswing.JTable;
import org.aswing.JTextArea;
import org.aswing.JTextField;
import org.aswing.JToggleButton;
import org.aswing.JToolBar;
import org.aswing.JTree;
import org.aswing.JViewport;
import org.aswing.JWindow;
import org.aswing.SoftBox;

/**
 * Privides public API allowed to access components created using AWML.
 * 
 * @author Igor Sadovskiy
 */
class org.aswing.awml.AwmlManager {
    
    /** Root namespace */
    private static var rootNamespace:AwmlNamespace;
    
    
    /**
     * Initializes root namespace and returns it.
     * 
     * @return the root namespace 
     */
    public static function getRootNamespace():AwmlNamespace {
        if (rootNamespace == null) {
            rootNamespace = new AwmlNamespace();
        }
        return rootNamespace;   
    }
    
    /**
     * getComponent(awmlID:String)<br>
     * getComponent(awmlID:String, namespaceName:String)<br>
     * <p>
     * Returns AWML {@link org.aswing.Component} instance by <code>awmlID</code>
     * within the specified namespace. If <code>namespaceName</code> isn't 
     * specified, searches for the first entrty of the component with the
     * passed-in <code>awmlID</code> in the root and its children namespaces.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.Component} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.Component}
     */
    public static function getComponent(awmlID:String, namespaceName:String):Component {
        var namespace:AwmlNamespace = (namespaceName != null) ?
             getRootNamespace().getNamespace(namespaceName) : getRootNamespace(); 
        return namespace.getComponent(awmlID);
    }

    /**
     * Returns AWML {@link org.aswing.Container} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.Container} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.Container}
     */
    public static function getContainer(awmlID:String, namespaceName:String):Container {
        return Container(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JFrame} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JFrame} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JFrame}
     */
    public static function getFrame(awmlID:String, namespaceName:String):JFrame {
        return JFrame(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JWindow} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JWindow} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JWindow}
     */
    public static function getWindow(awmlID:String, namespaceName:String):JWindow {
        return JWindow(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JTextField} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JTextField} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JTextField}
     */
    public static function getTextField(awmlID:String, namespaceName:String):JTextField {
        return JTextField(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JTextArea} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JTextArea} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JTextArea}
     */
    public static function getTextArea(awmlID:String, namespaceName:String):JTextArea {
        return JTextArea(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JSeparator} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JSeparator} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JSeparator}
     */
    public static function getSeparator(awmlID:String, namespaceName:String):JSeparator {
        return JSeparator(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JProgressBar} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JProgressBar} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JProgressBar}
     */
    public static function getProgressBar(awmlID:String, namespaceName:String):JProgressBar {
        return JProgressBar(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JLabel} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JLabel} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JLabel}
     */
    public static function getLabel(awmlID:String, namespaceName:String):JLabel {
        return JLabel(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JButton} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JButton} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JButton}
     */
    public static function getButton(awmlID:String, namespaceName:String):JButton {
        return JButton(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JToggleButton} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JToggleButton} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JToggleButton}
     */
    public static function getToggletButton(awmlID:String, namespaceName:String):JToggleButton {
        return JToggleButton(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JCheckBox} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JCheckBox} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JCheckBox}
     */
    public static function getCheckBox(awmlID:String, namespaceName:String):JCheckBox {
        return JCheckBox(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JRadioButton} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JRadioButton} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JRadioButton}
     */
    public static function getRadioButton(awmlID:String, namespaceName:String):JRadioButton {
        return JRadioButton(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JPanel} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JPanel} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JPanel}
     */
    public static function getPanel(awmlID:String, namespaceName:String):JPanel {
        return JPanel(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.Box} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.Box} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.Box}
     */
    public static function getBox(awmlID:String, namespaceName:String):Box {
        return Box(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.SoftBox} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.SoftBox} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.SoftBox}
     */
    public static function getSoftBox(awmlID:String, namespaceName:String):SoftBox {
        return SoftBox(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JToolBar} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JToolBar} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JToolBar}
     */
    public static function getToolBar(awmlID:String, namespaceName:String):JToolBar {
        return JToolBar(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JScrollBar} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JScrollBar} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JScrollBar}
     */
    public static function getScrollBar(awmlID:String, namespaceName:String):JScrollBar {
        return JScrollBar(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JSlider} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JSlider} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JSlider}
     */
    public static function getSlider(awmlID:String, namespaceName:String):JSlider {
        return JSlider(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JList} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JList} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JList}
     */
    public static function getList(awmlID:String, namespaceName:String):JList {
        return JList(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JComboBox} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JComboBox} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JComboBox}
     */
    public static function getComboBox(awmlID:String, namespaceName:String):JComboBox {
        return JComboBox(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JAccordion} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JAccordion} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JAccordion}
     */
    public static function getAccordion(awmlID:String, namespaceName:String):JAccordion {
        return JAccordion(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JTabbedPane} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JTabbedPane} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JTabbedPane}
     */
    public static function getTabbedPane(awmlID:String, namespaceName:String):JTabbedPane {
        return JTabbedPane(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JLoadPane} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JLoadPane} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JLoadPane}
     */
    public static function getLoadPane(awmlID:String, namespaceName:String):JLoadPane {
        return JLoadPane(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JAttachPane} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JAttachPane} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JAttachPane}
     */
    public static function getAttachPane(awmlID:String, namespaceName:String):JAttachPane {
        return JAttachPane(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JScrollPane} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JScrollPane} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JScrollPane}
     */
    public static function getScrollPane(awmlID:String, namespaceName:String):JScrollPane {
        return JScrollPane(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JViewport} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JViewport} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JViewport}
     */
    public static function getViewport(awmlID:String, namespaceName:String):JViewport {
        return JViewport(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JTable} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JTable} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JTable}
     */
    public static function getTable(awmlID:String, namespaceName:String):JTable {
        return JTable(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JTree} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JTree} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JTree}
     */
    public static function getTree(awmlID:String, namespaceName:String):JTree {
        return JTree(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JAdjuster} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JAdjuster} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JAdjuster}
     */
    public static function getAdjuster(awmlID:String, namespaceName:String):JAdjuster {
        return JAdjuster(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.JSpacer} instance by <code>awmlID</code>.
     * 
     * @param awmlID the AWML ID of the {@link org.aswing.JSpacer} to get
     * @param namespaceName (optional) the namespace to search component in
     * @return the AWML {@link org.aswing.JSpacer}
     */
    public static function getSpacer(awmlID:String, namespaceName:String):JSpacer {
        return JSpacer(getComponent(awmlID, namespaceName));
    }

    /**
     * Returns AWML {@link org.aswing.ButtonGroup} instance by its <code>id</code>.
     * 
     * @param id the ID of the {@link org.aswing.ButtonGroup} to get
     * @return the {@link org.aswing.ButtonGroup} instance or <code>null</code>
     * 
     * @see ButtonGroupManager#get
     */
    public static function getButtonGroup(id:String):ButtonGroup {
        return ButtonGroupManager.get(id);
    }
    
    
	/**
	 * addEventListener(id:String, object:Object)<br>
	 * addEventListener(id:String, class:Function)<br>
	 * <p>Registers object's instance or class as event listener within parser and
	 * associates it with the specified <code>id</code>.
	 * @param id the <code>id</code> of the instance or class to refer it from the AWML
	 * @param listener the reference to the class or object's instance
	 * @see AwmlParser#addEventListener 
	 */
	public static function addEventListener(id:String, listener:Object):Void {
		AwmlParser.addEventListener(id, listener);
	} 
	
	/**
	 * removeEventListener(id:String)<br>
	 * removeEventListener(object:Object)<br>
	 * removeEventListener(class:Function)<br>
	 * 
	 * <p>Unregister AWML event listener by specified <code>id</code> or reference to class
	 * or object's instance
	 * @param idOrListener the <code>id</code> or reference to the instance or class to remove
	 * @see AwmlParser#removeEventListener
	 */
	public static function removeEventListener(idOrListener:Object):Void {
		AwmlParser.removeEventListener(idOrListener);
	}
		
	/**
	 * Removes all registered AWML listeners.
	 * @see AwmlParser#removeAllEventListeners
	 */
	public static function removeAllEventListeners(Void):Void {
		AwmlParser.removeAllEventListeners();
	}
	    
    private function AwmlManager() {
        //  
    }
}